import java.io.*;
import java.util.*;
import local.util.MyPrint;

public class GenerateHtmlChart {

    private RandomAccessFile outputFileHandler;
    private static String symbol;
    private static String sid;
    private String industryMessage = null;
    private String upDownGrade     = null;
    private static String profileMessage="";
    private static String myNote="";
    private static String workDir ="C:/myProjects/apache2/htdocs/Stock/Charts/";
    private static String subDir ="";

    public static void main( String args[] ) {

	if (args.length < 2 ) {
	    System.out.println("================================================");
	    System.out.println("[1][Subdirectory][Tick][Note]");
	    System.out.println("[2][Subdirectory][Tick file]");
	    System.out.println("[3][Subdirectory][Tick List]");
	    System.out.println("================================================");
	    return;
	}

	RandomAccessFile rafh;
	GenerateHtmlChart gchart = new GenerateHtmlChart();
	TickSidFileLoader sidLoader = new TickSidFileLoader ();
	Hashtable hTab = null;
	Hashtable tickHash = null;
	Vector tickVector = new Vector ();
	String dataDir = "C:/myProjects/stock/Data/";
	String file_name = null;

	String choice = args[0];
	subDir = args[1];

	//Load Sid File into Hashtable
	System.out.println("GenerateHtmlChart: Loading Sid File.......");
	sidLoader.setFileName(dataDir+"AllTickSid.txt");
	sidLoader.loadTickSidFile();
	hTab = sidLoader.getTickHashTable();
	System.out.println("GenerateHtmlChart: Loading Sid File....Done!");

	//Load Tick File
	if (choice.equals("1")) {

	    symbol = args[2];
	    if (args.length==4)
		myNote = args[3];

	    sid = (String)hTab.get((Object)(symbol));
	    if (sid == null) {
		System.out.println("GenerateHtmlChart: Cant find Sid for:"+symbol+"....");
		System.exit(0);
	    }

	    gchart.openFile(symbol);
	    rafh = gchart.getOutputFileHandler();
	    gchart.addContent(rafh);
	    gchart.closeFile(rafh);
	}
	else if (choice.equals("3")) {

	    for (int j=2; j<args.length; j++) {
		symbol = args[j];

		sid = (String)hTab.get((Object)(symbol));
		if (sid == null) {
		    System.out.println("GenerateHtmlChart: Cant find Sid for:"+symbol+"....");
		    System.exit(0);
		}

		gchart.openFile(symbol);
		rafh = gchart.getOutputFileHandler();
		gchart.addContent(rafh);
		gchart.closeFile(rafh);
	    }

	}
	else if (choice.equals("2")) {
	    file_name = args[2];

	    File tickFile = new File (dataDir+file_name);
	
	    if (tickFile.isFile()) {
		System.out.println("GenerateHtmlChart: Loading Tick File.....");

		SymbolFileLoader fLoader = new SymbolFileLoader();
		fLoader.setFileName(dataDir+file_name);
		fLoader.loaderTickFile();
		tickHash = fLoader.getTickHashTable();
		tickVector = fLoader.getTickVector(); 
		System.out.println("GenerateHtmlChart: Done loading "+file_name+"!");

		String temp="";
		Enumeration enum = tickVector.elements();

		while (enum.hasMoreElements()) {
		    temp = (String)enum.nextElement();
		    if (!(temp.startsWith("<")|| (temp.endsWith(">")))) {
			symbol = temp.trim();
		    }
		    
		    System.out.println("GenerateHtmlChart: Seeking Sid for:"+symbol+"....");
		    sid = (String)hTab.get((Object)(symbol));
		
		    if(sid == null) {
			System.out.println("GenerateHtmlChart: Cant find Sid for:"+symbol+"....");
			System.exit(0);
		    }

		    System.out.println("GenerateHtmlChart: Found Sid For:"+symbol+"="+sid);
		    gchart.openFile(symbol);
		    System.out.println("GenerateHtmlChart: File For:"+symbol+" Has been opened for writing.");
		    rafh = gchart.getOutputFileHandler();
		    gchart.addContent(rafh);
		    System.out.println("GenerateHtmlChart: Html File For:"+symbol+" Has been created!");
		    gchart.closeFile(rafh);
		}
	    }
	    else {
		System.out.println("Can not find the file: "+tickFile.getName());
		System.exit(0);
	    }
	}
	
	GenerateHtmlJavaScript frame = new GenerateHtmlJavaScript();
	frame.setCurrentDir("Stock/Charts");
	frame.makeHtmlFrame();
	
    }

    public void generateHtmlFile ( String dir, String tick) {

	RandomAccessFile rafh;
	GenerateHtmlChart gchart = new GenerateHtmlChart();
	TickSidFileLoader sidLoader = new TickSidFileLoader ();
	Hashtable hTab = null;
	Hashtable tickHash = null;
	Vector tickVector = new Vector ();
	String dataDir = "C:/myProjects/stock/Data/";
      
	symbol    = tick;
	subDir    = dir;

	//Load Sid File into Hashtable
	System.out.println("GenerateHtmlChart: Loading Sid File.......");
	sidLoader.setFileName(dataDir+"AllTickSid.txt");
	sidLoader.loadTickSidFile();
	hTab = sidLoader.getTickHashTable();
	System.out.println("GenerateHtmlChart: Loading Sid File....Done!");

	sid = (String)hTab.get((Object)(symbol));
	if (sid == null) {
	    MyPrint.printLine("*",50);
	    System.out.println("GenerateHtmlChart: Cant find Sid for:"+symbol+"....");
	    MyPrint.printLine("*",50);
	    return;
	}

	gchart.openFile(symbol);
	rafh = gchart.getOutputFileHandler();
	gchart.addContent(rafh);
	gchart.closeFile(rafh);
	/*      
	GenerateHtmlFrame frame = new GenerateHtmlFrame();
	frame.makeHtmlFrame();
	*/
    }

    private void openFile(String tick) {
	RandomAccessFile output_file_handler;
	File subDirName = new File (workDir+subDir);;
	File fileName = new File (workDir+subDir+"/" + tick+".HTML");;
	if(subDirName.isDirectory()) {
 
	    try {
		output_file_handler = new RandomAccessFile( fileName, "rw" );
		setOutputFileHandler(output_file_handler);
	    }
	    catch ( IOException e ) {
		System.out.println("Can not create file under:"+subDir);
		System.exit(1);
	    }
	}
	else {
	    subDirName.mkdir();
	    try {
		output_file_handler = new RandomAccessFile( fileName, "rw" );
		setOutputFileHandler(output_file_handler);
	    }
	    catch ( IOException e ) {
		System.out.println("Can not create file under:"+subDir);
		System.exit(1);
	    }
	}
    }

    public RandomAccessFile getOutputFileHandler() {
	return outputFileHandler;
    }

    public void  setOutputFileHandler(RandomAccessFile fh) {
	outputFileHandler = fh;
    }

   private void closeFile(RandomAccessFile output_file_handler) 
   {
      try {
         output_file_handler.close();
      }
      catch( IOException ex ) {
         System.exit( 1 );
      }
   }

   public void addContent( RandomAccessFile output_file_handler )
   {
       Vector vInput = new Vector();
       String input = null;
       String first = symbol.substring(0,1);

       if (!symbol.equals("QQQ")) {
	   TickTable tb = new TickTable ();
	   industryMessage = tb.getTickIndustryName(symbol);

	   UpDownGrade ud = new UpDownGrade ();
	   System.out.println("GenerateHtmlChart: Finding Up/Dowm Grade for:"+symbol+"....");
	   upDownGrade = ud.getUpDownGradeMessage(symbol);

	   GetCompanyProfile gProfile = new GetCompanyProfile();

	   System.out.println("GenerateHtmlChart: Finding Enevt for:"+symbol+"....");

	   gProfile.connectToUrl("http://biz.yahoo.com/c/"+first.toLowerCase()+"/"+symbol.toLowerCase()+".html");

	   input = gProfile.getUrlReturn();

	   if (input == null) {
	       profileMessage = "Profile Not Found on Yahoo!";
	   }
	   else {
	       vInput = gProfile.putInVectorByLine (input);
	       vInput = gProfile.selectUsefulInfo (vInput);
	       profileMessage = gProfile.getEventMessage( vInput);
	       System.out.println("GenerateHtmlChart: Generating HTML file for:"+symbol+"...");
	       System.out.println("--------------------------------------------------");
	   }
       }
       else {
	   industryMessage="";
	   profileMessage = "";
       }

       profileMessage = "";

       try {
	   output_file_handler.writeBytes("<html><head><META HTTP-EQUIV=\"REFRESH\" CONTENT=\"15\">" +
			     "<title>"+symbol+"</title>" +
			     "<body bgcolor=\"#808080\"><font size='2'>"+myNote+"["+industryMessage+"]["+upDownGrade+"]["+
					  profileMessage+
			     "]</font>"+
			     "[<A href=\"http://biz.yahoo.com/p/"+first.toLowerCase()+"/"+symbol.toLowerCase()+".html\">"+
			     "<font size='2'>"+symbol+"</font></A>]"+
			     "[<A href=\"http://stockcharts.com/def/servlet/SC.web?c="+symbol+
			     ",uu[m,a]daclyiay[pb50!b200][vc60][iUb5!La12,26,9]&pref=G\">Stock Chart1</A>]"+
                             "[<A href=\"http://stockcharts.com/def/servlet/SC.web?c="+symbol+
                             ",uu[m,a]daclyiay[pb50!b200][vc60][iUk10!Ld20]&pref=G\">Stock Chart2</A>]"+
			     "[<A href=\"http://iw.thomsonfn.com/iwatch/cgi-bin/iw_ticker?t="+symbol+
			     "&range=30&mgp=0&i=3&hdate=&x=16&y=13=\">Tomase</A>]"+
			     "[<A href=\"/index/servlet/DeletFileServlet?dir="+
			      "/Stock/"+subDir+"&id="+symbol+".HTML\">Delete</A>]"+
			     "<TABLE><TR><TD>"+
			     "<IMG SRC=\"https://realtime.bigcharts.com/custom/datek-com/chart.asp?"+
			     "symb=nasdaq&time=1dy&freq=1mi&compidx=aaaaa%7E0&ma=0&maval=9&uf=0&lf=1&"+
			     "lf2=0&lf3=0&type=2&style=962&size=1&state=0&sid=3291&country=US&intMode=3&"+
			     "pageTitle=Chart+for+Nasdaq+Composite+Index&doChart=True&partner=0&"+
			     "mocktick=1&rand=5497\" BORDER=\"0\" WIDTH=\"500\" HEIGHT=\"264\"></TD>"+
			     "<TD><IMG SRC=\"https://realtime.bigcharts.com/custom/datek-com/chart.asp?"+
			     "symb="+symbol+"&time=1dy&freq=1mi&compidx=aaaaa%7E0&ma=0&maval=9&uf=0&lf=1&"+
			     "lf2=0&lf3=0&type=2&style=962&size=1&state=0&sid="+sid+"&country=US&"+
			     "intMode=3&pageTitle=Chart+for+Nasdaq+Composite+Index&doChart=True&"+
			     "partner=0&mocktick=1&rand=5497\"BORDER=\"0\" WIDTH=\"500\"HEIGHT=\"264\">"+
			     "</TD></TR>"+
			     "<TR><TD><IMG SRC=\"https://realtime.bigcharts.com/custom/datek-com/chart.asp?"+
			     "symb="+symbol+"&time=10dy&freq=1mi&compidx=aaaaa%7E0&ma=0&maval=9&uf=0&"+
			     "lf=1&lf2=0&lf3=0&type=2&style=962&size=1&state=0&sid="+sid+"&country=US&"+
			     "intMode=3&pageTitle=Chart+for+Nasdaq+Composite+Index&doChart=True&"+
			     "partner=0&mocktick=1&rand=5497\"BORDER=\"0\"WIDTH=\"500\"HEIGHT=\"264\"></TD>"+
			     "<TD><IMG SRC=\"https://realtime.bigcharts.com/custom/datek-com/chart.asp?"+
			     "symb="+symbol+"&time=6mo&freq=1dy&compidx=aaaaa%7E0&ma=0&maval=9&uf=0&"+
			     "lf=1&lf2=0&lf3=0&type=2&style=962&size=1&state=0&sid="+sid+"&country=US&"+
			     "intMode=3&pageTitle=Chart+for+Nasdaq+Composite+Index&doChart=True&"+
			     "partner=0&mocktick=1&rand=5497\"BORDER=\"0\"WIDTH=\"500\"HEIGHT=\"264\">"+
			     "</TD></TR>"+
			     "<TR><TD><IMG SRC=\"https://realtime.bigcharts.com/custom/datek-com/chart.asp?"+
			     "symb=nasdaq&time=10dy&freq=1mi&compidx=aaaaa%7E0&ma=0&maval=9&uf=0&lf=1&"+
			     "lf2=0&lf3=0&type=2&style=962&size=1&state=0&sid=3291&country=US&intMode=3&"+
			     "pageTitle=Chart+for+Nasdaq+Composite+Index&doChart=True&partner=0&"+
			     "mocktick=1&rand=5497\" BORDER=\"0\" WIDTH=\"500\" HEIGHT=\"264\"></TD>"+
			     "<TD><IMG SRC=\"https://realtime.bigcharts.com/custom/datek-com/chart.asp?"+
			     "symb=nasdaq&time=6mo&freq=1dy&compidx=aaaaa%7E0&ma=0&maval=9&uf=0&"+
			     "lf=1&lf2=0&lf3=0&type=2&style=962&size=1&state=0&sid=3291&country=US&"+
			     "intMode=3&pageTitle=Chart+for+Nasdaq+Composite+Index&doChart=True&"+
			     "partner=0&mocktick=1&rand=5497\"BORDER=\"0\"WIDTH=\"500\"HEIGHT=\"264\">"+
			     "</TD>"+
			     "</TR></TABLE>"+
			     "</body></html>");
       }
       catch ( IOException io ) {
	   System.out.println("GenerateHtmlChart Errot:"+io);
	   closeFile(output_file_handler);
       }
   }
}









